import * as _ from 'lodash'

const ACCOUNT_KEY = '_app_account'
const NAME_KEY = '_app_name'
const GENDER_KEY = '_app_gender'
const USER_ID_KEY = '_app_user_id'
const PASSWORD_KEY = '_app_password'
const USER_PHOTO_KEY = '_app_user_photo'
const TOKEN_KEY = '_app_token'

/**
 * 全局用户状态
 * @author chuck
 */
export default {
    namespaced: true,
    state: {
        _account: undefined,
		_password: undefined,
		_name: undefined,
		_gender: undefined,
		_userId: undefined,
		_userPhoto: undefined,
		_token: undefined
    },
    getters: {
        account (state) {
            let _account = state._account
            if (!_.isString(_account) || !_account.length) {
                _account = uni.getStorageSync(ACCOUNT_KEY)
            }
            return _account
        },
		password(state) {
		    let _password = state._password
		    if (!_.isString(_password) || !_password.length) {
		        _password = uni.getStorageSync(PASSWORD_KEY)
		    }
		    return _password
		},
        name(state) {
            let _name = state._name
            if (!_.isString(_name) || !_name.length) {
                _name = uni.getStorageSync(NAME_KEY)
            }
            return _name
        },
		gender(state) {
		    let _gender = state._gender
		    if (!_.isString(_gender) || !_gender.length) {
		        _gender = uni.getStorageSync(GENDER_KEY)
		    }
		    return _gender
		},
		userId (state) {
		    let _userId = state._userId
		    if (!_.isString(_userId) || !_userId.length) {
		        _userId = uni.getStorageSync(USER_ID_KEY)
		    }
		    return _userId
		},
		userPhoto(state) {
		    let _userPhoto = state._userPhoto
		    if (!_.isString(_userPhoto) || !_userPhoto.length) {
		        _userPhoto = uni.getStorageSync(USER_PHOTO_KEY)
		    }
		    return _userPhoto
		},
		token(state) {
		    let _token = state._token
		    if (!_.isString(_token) || !_token.length) {
		        _token = uni.getStorageSync(TOKEN_KEY)
		    }
		    return _token
		}
    },
    mutations: {
        /**
         * 保存用户数据
         * @param { any } state 模块局部状态 
         * @param {*} user 用户数据
         */
        save(state, user) {
            if (!user) {
                return
            }
            const {
                username,
				pwd,
				name,
				gender,
				id,
				photo,
                token
            } = user

            state._account = username
			state._password = pwd
            state._name = name
			state._gender = gender
			state._userId = id
			state._userPhoto = photo
            state._token = token

            uni.setStorageSync(ACCOUNT_KEY, username)
			uni.setStorageSync(PASSWORD_KEY, pwd)
            uni.setStorageSync(NAME_KEY, name)
			uni.setStorageSync(GENDER_KEY, gender)
			uni.setStorageSync(USER_ID_KEY, id)
			uni.setStorageSync(USER_PHOTO_KEY, photo)
            uni.setStorageSync(TOKEN_KEY, token)
        },
        /**
         * 清空用户数据
         */
        clear(state) {
            state._account = undefined
			state._password = undefined
            state._name = undefined
			state._gender = undefined
			state._userId = undefined
			state._userPhoto = undefined
            state._token = undefined

            uni.removeStorageSync(ACCOUNT_KEY)
			uni.removeStorageSync(PASSWORD_KEY)
			uni.removeStorageSync(NAME_KEY)
			uni.removeStorageSync(GENDER_KEY)
			uni.removeStorageSync(USER_ID_KEY)
			uni.removeStorageSync(USER_PHOTO_KEY)
            uni.removeStorageSync(TOKEN_KEY)
        }
    }
}